package com.situ.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.domain.entity.TProduct;
import com.situ.domain.dto.SearchPageQueryDTO;
import com.situ.domain.vo.ProductQueryVO;
import com.situ.service.ProductService;
import com.situ.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
@Tag(name = "商品接口")
public class ProductController {

    private final ProductService productService;

    /**
     * 获取新到好货
     * @return
     */
    @GetMapping("/getNewsProducts")
    @Cacheable(value = "index:product", key = "'getNewsProducts'")
    @Operation(summary = "获取新到好货接口")
    public Result getNewsProducts() {
        IPage<TProduct> page = productService
                .lambdaQuery()
                .orderByDesc(TProduct::getCreatedTime)
                .page(Page.of(1, 4));
        List<TProduct> records = page.getRecords();
        log.info("返回信息:{}",records);
        return Result.success(200, "获取新到好货成功", records);
    }

    /**
     * 获取热销商品
     * @return
     */
    @GetMapping("/getHotSaleProducts")
    @Cacheable(value = "index:product", key = "'getHotSaleProducts'")
    @Operation(summary = "获取热销商品接口")
    public Result getHotSaleProducts () {
        Page<TProduct> page = productService.lambdaQuery()
                .orderByAsc(TProduct::getPriority)
                .page(Page.of(1, 4));
        return Result.success(200, "获取热销商品成功", page.getRecords());
    }


    @GetMapping("/getProductById")
    @Operation(summary = "根据id获取商品接口")
    public Result getProductById (@RequestParam Integer id) {
        TProduct product = productService.getById(id);
        log.info("返回信息:{}",product);
        return Result.success(200, "获取商品成功", product);
    }

    /**
     * 分页搜索商品
     * @param searchKey
     * @return
     */
    @PostMapping("/searchProductsByPage")
    @Operation(summary = "分页搜索商品接口")
    public Result searchProductsByPage(@RequestBody SearchPageQueryDTO pageQueryDTO) {
        log.info("根据关键字搜索:{}", pageQueryDTO);
        ProductQueryVO productQueryVO = productService.searchProductsByPage(pageQueryDTO);
        return Result.success(200, "搜索成功", productQueryVO);
    }

}
